const Controllers = require('../../controllers/Controller.js').controller;
const formatTime3 = require('../../../utils/util.js').formatTime3
Page({
  data: {
    diyList:[],
    array:['套餐卡','充值卡','会员卡'],
    isPush:0,

    tabList:[
      {
        name:'未发布',
        id:0
      },{
        name:'已发布',
        id:1
      }
    ]
  },
  onShow: function (options) {
    this.getDiyList()
  },

  // 切换会员卡列表
  changeIsPush(e){
    let index=e.currentTarget.dataset.index
    this.setData({
      isPush:this.data.tabList[index].id
    })
    this.getDiyList()
  },

  // //跳转会员卡编辑页
  // toSetCard(e){
  //   let index=e.currentTarget.dataset.index
  //   if(this.data.diyList[index].type==0||this.data.diyList[index].type==5){
  //     wx.navigateTo({
  //       url: '/pages/diyActivity/adddiy/adddiy?id='+this.data.diyList[index].id,
  //     })
  //   }else{
  //     wx.navigateTo({
  //       url: '/pages/diyActivity/addcard/addcard?id='+this.data.diyList[index].id,
  //     })
  //   }
  // },

  // 发布会员卡
  releaseCard(e){
    let index=e.currentTarget.dataset.index
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.vipId=this.data.diyList[index].id
    Controllers.pushVip(value).then(res=>{
      wx.showToast({
        title: '发布成功',
      })
      this.getDiyList()
    })
  },


  //删除会员卡
  delVipCard(e){
    let index=e.currentTarget.dataset.index
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.vipId=this.data.diyList[index].id
    let that=this

    wx.showModal({
      title: '提示',
      content: '是否确认删除？',
      success (res) {
        if (res.confirm) {
          Controllers.vipdelete(value).then(res=>{
            wx.showToast({
              title: '删除成功',
            })
            that.getDiyList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //提前结束活动
  endActivity(e){
    let index=e.currentTarget.dataset.index
    let value={}
    value.shopId=wx.getStorageSync('shopId')
    value.vipId=this.data.diyList[index].id
    let that=this

    wx.showModal({
      title: '提示',
      content: '是否提结束？',
      success (res) {
        if (res.confirm) {
          Controllers.setCardOver(value).then(res=>{
            wx.showToast({
              title: '操作成功',
            })
            that.getDiyList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },


  getDiyList(){
    Controllers.shopList({shopId:wx.getStorageSync('shopId'),isPush:this.data.isPush}).then(res=>{
      this.setData({
        diyList:res.data
      })
    })
  },
  addcard(){
    wx.navigateTo({
      url: "/pages/diyActivity/adddiy/adddiy"
    });
  },
  godetail(e){
    wx.navigateTo({
      url: "/pages/diyActivity/detail/detail?id="+e.currentTarget.dataset.id
    });
  },

  // 跳转已售列表
  goSellList(e){
    wx.navigateTo({
      url: "/pages/diyActivity/sellList/sellList?id="+e.currentTarget.dataset.id+"&name="+e.currentTarget.dataset.name
    });
  },

  bindPickerChange(e){
    if(e.detail.value==0){
      wx.navigateTo({
        url: "/pages/diyActivity/adddiy/adddiy?type=0"
      });
    }else if(e.detail.value==2){
      wx.navigateTo({
        url: "/pages/diyActivity/adddiy/adddiy?type=5"
      });
    }else{
      wx.navigateTo({
        url: "/pages/diyActivity/addcard/addcard"
      });
    }
  },
})